package Tool;

import Javabean.Staff;
import Javabean.User;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class MySQL_ArraylistCopy {
    public static void arrayListcopy(Connection conn,ArrayList arrayList ,String name) throws SQLException {
        //定义sql
        //查询
        String sql = "select * from " + name;

        //获取执行sql的对象 Stamant
        Statement stmt = conn.createStatement();
        //执行sql
        ResultSet rs = stmt.executeQuery(sql);

        //处理结果
        while (rs.next()) {
            User user = new User();
            Staff staff = new Staff();
            if (name.equals("user")){
                user.setID(rs.getString(1));
                user.setPassWord(rs.getString(2));
                arrayList.add(user);

            } else if (name.equals("staff")) {
                staff.setId(rs.getInt(1));
                staff.setName(rs.getString(2));
                staff.setSex(rs.getString(3));
                staff.setBrithday(rs.getString(4));
                staff.setAge(rs.getInt(5));
                staff.setMarry(rs.getInt(6));
                staff.setPlace(rs.getString(7));
                staff.setStaff(rs.getString(8));
                staff.setLevel(rs.getInt(9));
                staff.setSalary(rs.getDouble(10));
                arrayList.add(staff);
            }
        }
        //释放资源
        rs.close();
        stmt.close();
    }
}
